<?php
$password="";
$username="";
$name="";
include '../models/Models.php';

if(isset($_POST['dangnhap']))
{
    $name=  trim($_POST['name']);
        $username=trim($_POST['username']);
        $password=trim($_POST['password']);
    if(get_magic_quotes_gpc())
    {
        $name=  trim(mysqli_real_escape_string($_POST['name']));
        $username=trim(mysqli_real_escape_string($_POST['username']));
        $password=trim(mysqli_real_escape_string($_POST['password']));
    }
$error=array();
if(strlen($password)==0)
    array_push($error, "Hãy nhập vào");
if(strlen($username)==0)
    array_push($error, "Hãy nhập vào");
if(strlen($name)==0)
    array_push($error, "Hãy nhập vào");
if (count($error) == 0) {
    array_push($error, "ko có lỗi");
    if(isset($_POST['dangnhap']))
    {        
        $f=new Models();
        $f->insertUser($name, $username, $password);        
       header('Location:listAll.php');
    }
}
else {    
    $output="";
     foreach ($error as $val)
    {
        $output.="<p class='output'>$val</p>";
    }
    echo $output;      
    }
    }
?>

<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
  <script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

 <script>  
  // When the browser is ready...
  
  $( document ).ready(function() {
  
    // Setup form validation on the #register-form element
    $("#register-form").validate({
    
        // Specify the validation rules
        rules: {
            name: "required",           
            email: {
                required: true,
                email: true
            },
            username: "required",
            password: {
                required: true,
                minlength: 5
            }
        },
        
        // Specify the validation error messages
        messages: {
            name: "Please enter your name",
            gender: "Please specify your gender",
            address: "Please enter your address",
            email: "Please enter a valid email address",
            username: "Please enter a valid username",
            password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            }
        },
        
        submitHandler: function(form) {
            form.submit();
        }
    });

  });
  
  </script>
  
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Create user</title>
         <link href="Styles/menu-ngang.css" rel="stylesheet" type="text/css" />
       
    </head>
    <body>
        
        
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="register-form">
            <fieldset style="width: 300px;margin: 0 auto;">
                <legend>Đăng nhập</legend>
                    <div>
                        <label>Họ Tên</label>
                        <input type="text" name="name" value="<?php if(isset($_POST['name'])) echo $_POST['name']; ?>" />
                    </div>
                    <div>
                        <label>Tên đăng nhập</label>
                        <input type="text" name="username" value="<?php if(isset($_POST['username'])) echo $_POST['username'];?>" />
                    </div>
                    <div>
                        <label>Mật khẩu</label>
                        <input type="text" name="password" value="<?php if(isset($_POST['password'])) echo $_POST['password'];?>" />
                    </div>
                    <div>
                        <label>Hoạt động</label>
                        <input type="radio" name="radio" value="yes" class="radio" <?php if (isset($_POST['radio']) && $_POST['radio'] == 'yes'): ?>checked='checked'<?php endif; ?> /> Yes
                        <input type="radio" name="radio" value="no"  class="radio" <?php if (isset($_POST['radio']) && $_POST['radio'] ==  'no'): ?>checked='checked'<?php endif; ?> /> No                     
                    </div>
                <div>
                    <input type="submit" value="Thêm" name="dangnhap" /> </div>
            </fieldset>
            
        </form>       
    </body>
</html>
